<template>
  <div>
    <el-dialog
      title="修改目录"
      :visible="editSubjectDialog"
      width="400px"
      @close="onClose"
      >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item v-if="!$route.query.id" label="所属学科"  >
        <el-select v-model="form.subjectID" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName" >
        <el-input size="small" v-model.trim="form.directoryName" v-focus placeholder="请输入目录名称"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="editDirectorysName">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { update } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'

export default {
  data () {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      value: true,
      rules: {
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      options: []
    }
  },
  props: {
    // 对话框显示隐藏
    editSubjectDialog: {
      type: Boolean,
      required: true
    },
    // 每一行的数据
    directorysInfo: {
      type: Object,
      required: true
    }
  },
  created () {
    this.loadDirectorysInfo()
    this.getSubjectList()
  },

  methods: {
    // 添加学科
    async editDirectorysName () {
      try {
        await this.$refs.form.validate()
        this.form.id = this.directorysInfo.id
        this.form.subjectID = this.directorysInfo.subjectID
        const res = await update(this.form)
        console.log(res)
        this.$emit('getDirectory')
        this.$emit('update:editSubjectDialog', false)
        this.$message.success('亲，修改成功哟！')
        this.onClose()
      } catch (err) {
      }
    },
    // 关闭弹框
    onClose () {
      this.$emit('update:editSubjectDialog', false)
      this.$refs.form.resetFields()
      this.form.directoryName = ''
    },
    // 加载弹框
    loadDirectorysInfo () {
      this.form.directoryName = this.directorysInfo.directoryName
    },
    // 获取所属学科
    async getSubjectList () {
      const { data } = await simple()
      console.log(data)
      this.options = data
      this.form.subjectID = this.directorysInfo.subjectID
    }
  }
}
</script>

<style scoped lang='less'>
.el-form-item {
  /deep/.el-table__body-wrapper{
    /deep/.el-table__body{
      /deep/.el-form-item__label {
      width: 80px;
      }
    }
  }
  }
/deep/.el-form-item__content {
  margin-left: 80px;
}
  /deep/.el-input {
    display: flex;
    width: 280px !important;
  }
</style>
